﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace TY.UC.Common.Extension
{
    /// <summary>
    /// json序列化辅助类（基于NewtonSoft）
    /// </summary>
    public static class JsonExtension
    {
        ///// <summary>
        /////json解析对象
        ///// </summary>
        ///// <typeparam name="T">类型</typeparam>
        ///// <param name="json">json字符串</param>
        ///// <returns>对象实体</returns>
        //public static T JsonDe<T>(this string json)
        //{
        //    return JsonConvert.DeserializeObject<T>(json);
        //}

        ///// <summary>
        ///// 对象转换为json
        ///// </summary>
        ///// <param name="obj">对象</param>
        ///// <returns>json字符串</returns>
        //public static string JsonSe(this object obj)
        //{
        //    return JsonConvert.SerializeObject(obj);
        //}
        ///// <summary>
        ///// 对象转换为json（属性值全部为小写）
        ///// </summary>
        ///// <param name="obj">对象</param>
        ///// <returns></returns>
        //public static string JsonSeLower(this object obj)
        //{
        //    JsonSerializerSettings jss = new JsonSerializerSettings {ContractResolver = PropertyLowerCase.Instance};
        //    return JsonConvert.SerializeObject(obj, jss);
        //}
        ///// <summary>
        ///// 对象转化为linqobject
        ///// </summary>
        ///// <param name="json">jsos字符串</param>
        ///// <returns></returns>
        //public static JObject JsonLinq(this string json)
        //{
        //    return JObject.Parse(json);
        //}

        ///// <summary>
        ///// 获取json值，key不区分大小写
        ///// </summary>
        ///// <param name="jobj">jobject实体</param>
        ///// <param name="key">键</param>
        ///// <returns>值</returns>
        //public static string Value(this JObject jobj,string key)
        //{
        //    return jobj.GetValue(key, System.StringComparison.InvariantCultureIgnoreCase) + "";
        //}

    }

    /// <summary>
    /// json key小写转换
    /// </summary>
    public class PropertyLowerCase : Newtonsoft.Json.Serialization.DefaultContractResolver
    {
        public static readonly PropertyLowerCase Instance = new PropertyLowerCase();
        protected override string ResolvePropertyName(string propertyName)
        {
            return propertyName.ToLower();
        }
    }
}
